package com.contact.util.util.validation;

import java.util.List;

import com.contact.util.StringUtils;

public final class ValidationUtils {//NOPMD

    private ValidationUtils() {
        //singleton
    }

    public static void checkRequiredParameter(Object param, String paramName) throws ValidationException {
        if (param == null) {
            throw new ValidationException(String.format("Required parameter %s is missing.", paramName));
        }
        if (param.getClass().isAssignableFrom(String.class)) {
            String sValue = (String) param;
            if (StringUtils.isEmpty(sValue)) {
                throw new ValidationException(String.format("Required parameter %s is missing.", paramName));
            }
        }
    }

    /**
     * Check all the fields in an import file to see that they comply with length limitation.
     * @param fields
     * @throws ValidationException if value is beyond the allowed length
     */
    public static void checkFieldLengths(List<FieldCharacterLength> fields) throws ValidationException {
        for (FieldCharacterLength f : fields) {
            if (f.getValue() != null && f.getValue().length() > f.getAllowedLength()) {
                throw new ValidationException(String.format("Field %s is beyond the maximum length of %s.", f.getFieldName(), f.getAllowedLength()));
            }
        }
    }

    /**
     * Check the length of the values to the left and right of the decimal. Will also throw an error if any
     * characters are anything other than the decimal or a digit.
     * @param fieldName
     * @param value
     * @param leftValue
     * @param rightValue
     * @throws ValidationException
     */
    public static void checkDecimalLength(String fieldName, String value, int leftValue, int rightValue) throws ValidationException {//NOPMD

        if (value == null) {
            return;
        } else {
            value = value.trim();
            if (value.length() == 0) {
                return;
            }
        }
        char[] characters = value.toCharArray();
        for (char c : characters) {
            if (c != '.' && !Character.isDigit(c)) {
                throw new ValidationException(String.format("Invalid decimal value (%s) for field %s.", value, fieldName));
            }
        }
        String[] values = value.split("\\.");
        if (values.length > 0 && values[0] != null && values[0].length() > leftValue) {
            throw new ValidationException(String.format(
                    "Digits to the left of decimal on field %s is beyond the maximum length of %s.", fieldName, leftValue));
        }

        if (values.length == 2 && values[1] != null && values[1].length() > rightValue) {
            throw new ValidationException(String.format(
                    "Digits to the right of decimal on field %s is beyond the maximum length of %s.", fieldName, rightValue));
        }
    }
}
